﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Interop;

//steamswap's main interface, coded by whiplash except where "special thanks" are given.
//licensed under gnu gpl v3

namespace steamswap {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        private string folder1;
        private string folder2;
        private int leftItemCount;
        private int rightItemCount;
        public MainWindow() {
            InitializeComponent();

            //get a count of the number of files to track per side
            //for now, it'll be hard-coded
            leftItemCount = 1;
            rightItemCount = 2;
            folder1 = @"c:\test1";
            folder2 = @"c:\test2";
        }

        //Special thanks to Daniel Tan http://novalistic.com/ http://wpfdraggableglass.codeplex.com/
        #region Aero Glass extensions

        private IntPtr hwnd;
        private HwndSource hsource;

        private void AdjustGlassFrame() {
            if ( DwmApiInterop.IsCompositionEnabled() ) {
                ExtendGlassIntoClientArea( 600, 600, 300, 300 );
            }
            else {
                FallbackPaint();
            }
        }

        private void ExtendGlassIntoClientArea( int left, int right, int top, int bottom ) {
            var margins = new MARGINS {
                cxLeftWidth = left,
                cxRightWidth = right,
                cyTopHeight = top,
                cyBottomHeight = bottom
            };
            int hresult = DwmApiInterop.ExtendFrameIntoClientArea( hwnd, ref margins );

            if ( hresult == 0 ) {
                hsource.CompositionTarget.BackgroundColor = Colors.Transparent;
                Background = Brushes.Transparent;
            }
            else {
                throw new InvalidOperationException( "Could not extend glass window frames in the main window." );
            }
        }

        private void FallbackPaint() {
            Background = Brushes.DarkGray;
        }

        private bool IsOnGlass( int lParam ) {
            int x = lParam << 16 >> 16, y = lParam >> 16;
            Point point = PointFromScreen( new Point( x, y ) );
            var result = VisualTreeHelper.HitTest( windowGrid, point );

            if ( result != null ) {
                return result.VisualHit == windowGrid;
            }

            return true;
            //return VisualTreeHelper.HitTest(windowGrid, point).VisualHit == windowGrid;
        }

        private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled ) {
            switch ( msg ) {
                // Ignore clicks if desktop composition isn't enabled
                case DwmApiInterop.WM_NCHITTEST:
                    if ( DwmApiInterop.IsCompositionEnabled()
                        && DwmApiInterop.IsOnClientArea( hwnd, msg, wParam, lParam )
                        && IsOnGlass( lParam.ToInt32() ) ) {
                        handled = true;
                        return new IntPtr( DwmApiInterop.HTCAPTION );
                    }

                    return IntPtr.Zero;

                // Also toggle glass painting on this window when desktop composition is toggled
                case DwmApiInterop.WM_DWMCOMPOSITIONCHANGED:
                    try {
                        AdjustGlassFrame();
                    }
                    catch ( InvalidOperationException ) {
                        FallbackPaint();
                    }
                    return IntPtr.Zero;

                default:
                    return IntPtr.Zero;
            }
        }
        private void Window_SourceInitialized( object sender, EventArgs e ) {
            try {
                if ( ( hwnd = new WindowInteropHelper( this ).Handle ) == IntPtr.Zero ) {
                    throw new InvalidOperationException( "Could not get window handle for the main window." );
                }

                hsource = HwndSource.FromHwnd( hwnd );
                hsource.AddHook( WndProc );

                AdjustGlassFrame();
            }
            catch ( InvalidOperationException ) {
                FallbackPaint();
            }
        }

        #endregion
    }

}
